﻿using DosTexPrj.Core;
using NHibernate;

namespace DosTexPrj.Implement.NHibernate
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly ISessionFactory _sessionFactory;
        private readonly ITransaction _transaction;

        public ISession Session { get; private set; }

        public UnitOfWork(ISessionFactory sessionFactory)
        {
            this._sessionFactory = sessionFactory;
            Session = this._sessionFactory.OpenSession();
            this._transaction = Session.BeginTransaction();
        }

        public void Commit()
        {
            if (this._transaction.IsActive)
                this._transaction.Commit();
        }

        public void Rollback()
        {
            if (this._transaction.IsActive)
                this._transaction.Rollback();
        }

        public void Dispose()
        {
            Session.Close();
            Session = null;
        }
    }
}